package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.service.FileUploadService;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @author XIN
 * 2023/4/20
 */
@Slf4j
@Service
public class FileUploadServiceimpl implements FileUploadService {

	@Autowired
	private MinioClient minioClient;
	
	@Value("${minio.bucketName}")
	private String bucketName;
	
	
	@Value("${minio.endpointUrl}")
	private String endpointUrl;
	
	/**
	 * 文件上传管理
	 * @param file
	 * @return
	 */
	@Override
	public String fileUpload(MultipartFile file) {
		try {
			boolean found =
					minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
			if (!found) {
				minioClient.makeBucket(
						MakeBucketArgs.builder()
								.bucket(bucketName)
								.build());
			}
			String fileName = System.currentTimeMillis()+UUID.randomUUID().toString().replaceAll("-", "");
			String contentType = file.getContentType();
			minioClient.putObject(
					PutObjectArgs.builder().bucket(bucketName).object(fileName).stream(
									file.getInputStream(), file.getSize(), -1)
							.contentType(contentType)
							.build());
			return endpointUrl+"/"+bucketName+"/"+fileName;
		}catch (Exception e){
			e.printStackTrace();
			log.error("文件上传失败:{}", e);
			throw new RuntimeException("文件上传失败!");
		}
	}
	
}
